<template>
  <ele-modal width="880px" :visible="visible" :append-to-body="true" :close-on-click-modal="true"
    custom-class="ele-dialog-form" :title="isUpdate ? '修改app' : '添加app'" @update:visible="updateVisible">
    <el-form ref="form" :model="form" :rules="rules" label-width="82px">
      <el-row :gutter="15">
        <el-col :md="12" :sm="12" :xs="24">
          <el-form-item label="类型" prop="type">
            <el-select v-model="form.type" placeholder="请选择类型" style="width: 100%">
              <el-option :value="1" label="推荐">推荐</el-option>
              <el-option :value="2" label="破解">破解</el-option>
              <el-option :value="3" label="看片">看片</el-option>
              <el-option :value="4" label="视频">视频</el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="12" :xs="24">
          <el-form-item label="状态" prop="status">
            <el-select v-model="form.status" placeholder="请选择状态" style="width: 100%">
              <el-option :value="0" label="隐藏">隐藏</el-option>
              <el-option :value="1" label="显示">显示</el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <!-- <el-col :md="12" :sm="12" :xs="24">
          <el-form-item label="底部悬浮" prop="isBottom">
            <el-select v-model="form.isBottom" placeholder="请选择状态" style="width: 100%">
              <el-option :value="0" label="正常">正常</el-option>
              <el-option :value="1" label="悬浮">悬浮</el-option>
            </el-select>
          </el-form-item>
        </el-col> -->
        <el-col :md="12" :sm="12" :xs="24">
          <el-form-item label="跳转地址" prop="href">
            <el-input v-model="form.href" placeholder="请输入应用地址" />
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="12" :xs="24">
          <el-form-item label="三方图片" prop="remoteUrl">
            <el-input v-model="form.remoteUrl" placeholder="请输入三方图片地址" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-col :md="24" :sm="24" :xs="24">
          <el-form-item label="描述" prop="desc">
            <el-input v-model="form.desc" type="textarea" placeholder="请输入描述" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-col :md="12" :sm="12" :xs="24">
          <el-form-item label="广告图片" prop="img">
            <el-upload class="avatar-uploader" style="border: 1px solid #999;width:100px" action="#" :multiple="false"
              :show-file-list="false" :limit="1" :auto-upload="false" :on-change="onUloadChange">
              <img v-if="form.img"
                :src="(form.img.indexOf('/advertise/') === -1 ? '' : $store.state.resourceUrl) + form.img"
                class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script>
import { fileToBase64 } from "@/utils/formate";
import { editAdvertise, addAdvertise } from "@/api/advertiseList";
export default {
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    const defaultForm = {
      _id: null,
      name: "",
      type: 1,
      status: 1,
      href: "",
      isBottom:undefined,
      desc: "",
      index: 1,
      remoteUrl: "",
      img: ""
    };
    return {
      defaultForm,
      // 表单数据
      form: { ...defaultForm },
      // 表单验证规则
      rules: {
        name: [
          {
            required: true,
            message: '请输入应用名称',
            trigger: 'blur'
          }
        ],
        type: [
          {
            required: true,
            message: '请选择类型',
            trigger: 'blur'
          }
        ],
        status: [
          {
            required: true,
            message: '请选择状态',
            trigger: 'blur'
          }
        ],
        href: [
          {
            required: true,
            message: '请输入应用地址',
            trigger: 'blur'
          }
        ],
        index: [
          {
            required: true,
            message: '请输入顺序',
            trigger: 'blur'
          }
        ],
        img: [
          {
            required: true,
            message: '请上传应用图标',
            trigger: 'change'
          }
        ]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    visible(visible) {
      if (visible) {
        if (this.data) {
          let data = { ...this.data }
          this.$util.assignObject(this.form, {
            ...data,
          });
          this.isUpdate = true;
        } else {
          this.isUpdate = false;
        }
      } else {
        this.$refs.form.clearValidate();
        this.form = { ...this.defaultForm };
      }
    }
  },
  methods: {
    // 上传图片
    onUloadChange(file, _, item) {
      if (file.size >= 1024 * 1024 * 400) {
        this.$message.error("最大上传限制400MB！");
        return;
      }
      fileToBase64(file.raw).then(res => {
        this.form.img = res;
      })
    },
    /* 保存编辑 */
    save() {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return false;
        }
        this.loading = true;
        const data = {
          ...this.form
        };


        // return
        const saveOrUpdate = this.isUpdate ? editAdvertise : addAdvertise;
        saveOrUpdate(data)
          .then((res) => {
            console.log('结果', res);
            this.loading = false;
            this.$message.success('保存成功');
            this.updateVisible(false);
            this.$emit('done');
          })
          .catch((msg) => {
            this.loading = false;
            this.$message.error(msg);
          });
      })
    },


    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>